using Dialog;
using Dialog.Data;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UI;
using UnityEngine;
using UnityEngine.UI;

public class DialogScenePanel : BasePanel<DialogScenePanel>
{
    public TextMeshProUGUI roleText;
    public TextMeshProUGUI contentText;

    public Image background;
    public Image portraitLeft;
    public Image portraitRight;
    public Image portraitMiddle;

    public GameObject choiceContainer;
    public Button choiceButtonPrefab;

    DialogueRuntime runtime;


    private void Start()
    {
        SetCurrentDialogue("scene_1");
    }


    public void Init(DialogueRuntime runtime)
    {
        this.runtime = runtime;
    }

    public void SetBackground(string name)
    {
        Debug.Log("Set BG: " + name);
    }

    public void PlayBGM(string name)
    {
        Debug.Log("Play BGM: " + name);
    }

    public void ShowTitle(string title)
    {
        Debug.Log("Title: " + title);
    }

    public void ShowDialogue(string role, string portrait, string position, string text)
    {
        roleText.text = role;
        contentText.text = text;

        Debug.Log($"{role} {portrait} {position} : {text}");
    }

    public void ShowChoices(List<ChoiceItem> choices, System.Action<int> onSelect)
    {
        foreach (Transform child in choiceContainer.transform)
            Destroy(child.gameObject);

        for (int i = 0; i < choices.Count; i++)
        {
            int idx = i;
            var btn = Instantiate(choiceButtonPrefab, choiceContainer.transform);
            btn.GetComponentInChildren<TextMeshProUGUI>().text = choices[i].text;
            btn.onClick.AddListener(() =>
            {
                onSelect(idx);
            });
        }
    }

    public void FadeScreen()
    {
        Debug.Log("Fade Screen");
    }

    public void SetCurrentDialogue(string dialoguePath)
    {
        var text = Resources.Load<TextAsset>(dialoguePath);
        var script = NewDialogManager.Instance.PhaseDialogData(text.text);

        runtime.ui = this;
        runtime.script = script;
        runtime.StartDialogue(0);
    }
}
